home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / unzup41e.zip / MAPNAME.C < prev    next >
C/C++ Source or Header  |  1991-03-18  |  13KB  |  301 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   mapname.c
  4.  
  5.   This routine changes DEC-20, VAX/VMS, and DOS-style filenames into normal
  6.   Unix names (and vice versa, in some cases); it also creates any necessary 
  7.   directories, if the -d switch was specified.
  8.  
  9.   ---------------------------------------------------------------------------
  10.  
  11.   Action:  renames argument files as follows:
  12.  
  13.      strips Unix and PKZIP DOS path name from front (up to rightmost '/') if
  14.        present.
  15.      strips DEC device:, node:: names from front (up to rightmost ':') if
  16.        present.  (This also takes care of any DOS drive: artifacts.)
  17.      strips DEC-20 <directory> or VMS [directory] name if present.
  18.      strips DEC-20 version number from end (everything after 2nd dot) if
  19.        present.
  20.      strips VMS generation number from end (everything after ';') if present,
  21.        unless "-V" switch specified.
  22.      honors DEC-20 CTRL-V quote for special characters.
  23.      discards unquoted unprintable characters.
  24.      [VMS] converts Unix-style pathnames to VMS style.
  25.  
  26.      Returns 0 if no error, 1 if filename truncated, 2 for any other error.
  27.  
  28.   ---------------------------------------------------------------------------
  29.  
  30.   Author:  David Kirschbaum, 25 Apr 90
  31.      (Based on good old xxu.c by Frank da Cruz, CUCCA)
  32.      Subsequent tweaks by Bill Davidsen, James Dugal, Larry Jones,
  33.      Mark Edwards, Greg Roelofs, Antoine Verheijen.
  34.  
  35.   ---------------------------------------------------------------------------
  36.  
  37.   Dot notes:
  38.  
  39.      - Unix allows multiple dots in directory names; MS-DOS and OS/2 FAT
  40.        allow one; VMS does not allow any.  Things are almost as bad with
  41.        regular filenames (VMS allows a single dot but TOPS-20 allows two,
  42.        if you count the one in front of the version number).  As of v4.04,
  43.        mapname converts directory-name dots to underscores on VMS, but it
  44.        otherwise leaves the dots alone.  Since it is now possible to create
  45.        zipfiles under Unix, this whole routine pretty much needs to be
  46.        rewritten (different rules for each output OS and for different 
  47.        parts of the path name).
  48.      - If each zip program stores local-format names (like the coming VMS
  49.        one will), it would probably be best to convert to an intermediate 
  50.        format first (assuming we're not extracting under the same OS as
  51.        that under which the zipfile was created), then from that to the 
  52.        current operating system's format.
  53.  
  54.   ------------------------------------------------------------------------- */
  55.  
  56.  
  57. #include "unzip.h"
  58.  
  59.  
  60. /********************/
  61. /*  Mapname Defines */
  62. /********************/
  63.  
  64. #ifdef VMS
  65. #define PERMS   0
  66. #else
  67. #define PERMS   0777
  68. #endif
  69.  
  70. #ifndef NO_MKDIR
  71. #ifdef DOS_OS2
  72. #if (_MSC_VER >= 600)           /* have special MSC mkdir prototype */
  73. #include <direct.h>
  74. #else                           /* own prototype because dir.h conflicts?? */
  75. int mkdir(const char *path);
  76. #endif                          /* !(MSC 6.0 or later) */
  77. #define MKDIR(path,mode)   mkdir(path)
  78. #else                           /* !DOS_OS2 */
  79. #define MKDIR(path,mode)   mkdir(path,mode)
  80. #endif
  81. #endif                          /* !NO_MKDIR */
  82.  
  83. /***************************/
  84. /*  Function mapped_name() */
  85. /***************************/
  86.  
  87. mapped_name()
  88. {
  89. #ifdef NO_MKDIR
  90.     char command[FILNAMSIZ+40]; /* buffer for system() call */
  91. #endif
  92. #ifdef VMS
  93.     int stat_val;               /* temp. holder for stat() return value */
  94.     char *dp, *xp;              /* ptrs to directory name */
  95. #endif
  96.     char name[FILNAMSIZ];       /* file name buffer */
  97.     char *pp, *cp, *cdp;        /* character pointers */
  98.     char delim = '\0';          /* Directory Delimiter */
  99.     int dc = 0;                 /* Counters */
  100.     int quote = FALSE;          /* Flags */
  101.     int indir = FALSE;
  102.     int done = FALSE;
  103.     register int workch;        /* hold the character being tested */
  104.  
  105.  
  106.  
  107. /*---------------------------------------------------------------------------
  108.     Initialize various pointers and counters and stuff.
  109.   ---------------------------------------------------------------------------*/
  110.  
  111. #ifdef MAP_DEBUG
  112.     fprintf(stderr, "%s ", filename);   /* echo name of this file */
  113. #endif
  114.     pp = name;                  /* Point to translation buffer */
  115.     *name = '\0';               /* Initialize buffer */
  116.  
  117.     if (dflag) {                /* -d => retain directory structure */
  118.         cdp = (char *) malloc(strlen(filename) + 3);  /* place for */
  119.         if (cdp == NULL) {      /*   holding directory name */
  120.             fprintf(stderr, "malloc failed in conversion of [%s]\n", filename);
  121.             return (2);
  122.         }
  123. #ifdef VMS
  124.         *cdp++ = '[';
  125.         xp = cdp;               /* always points to last non-NULL char */
  126.         *cdp++ = '.';
  127. #endif
  128. #ifdef MACOS
  129.         *cdp = ':';             /* the Mac uses ':' as a directory separator */
  130.         cdp[1] = '\0';
  131. #else
  132.         *cdp = '\0';
  133. #endif
  134.     }
  135.     dc = 0;                     /* Filename dot counter */
  136.  
  137. /*---------------------------------------------------------------------------
  138.     Begin main loop through characters in filename.
  139.   ---------------------------------------------------------------------------*/
  140.  
  141.     for (cp = filename; (workch = *cp++) != 0  &&  !done;) {
  142.  
  143.         if (quote) {            /* If this char quoted... */
  144.             *pp++ = workch;     /*  include it literally. */
  145.             quote = FALSE;
  146.         } else if (indir) {     /* If in directory name... */
  147.             if (workch == delim)
  148.                 indir = FALSE;  /*  look for end delimiter. */
  149.         } else
  150.             switch (workch) {
  151.             case '<':           /* Discard DEC-20 directory name */
  152.                 indir = TRUE;
  153.                 delim = '>';
  154.                 break;
  155.             case '[':           /* Discard VMS directory name */
  156.                 indir = TRUE;
  157.                 delim = ']';
  158.                 break;
  159.             case '/':           /* Discard Unix path name... */
  160.             case '\\':          /*  or MS-DOS path name...
  161.                                  *  UNLESS -d flag was given. */
  162.                 /*
  163.                  * Special processing case:  if -d flag was specified on
  164.                  * command line, create any necessary directories included
  165.                  * in the pathname.  Creation of directories is straight-
  166.                  * forward on BSD and MS-DOS machines but requires use of
  167.                  * the system() command on SysV systems (or any others which
  168.                  * don't have mkdir()).  The stat() check is necessary with
  169.                  * MSC because it doesn't have an EEXIST errno, and it saves
  170.                  * the overhead of multiple system() calls on SysV machines.
  171.                  */
  172.  
  173.                 if (dflag) {
  174.                     *pp = '\0';
  175. #ifdef VMS
  176.                     dp = name;
  177.                     while (*++xp = *dp++)   /* copy name to cdp, while */
  178.                         if (*xp == '.')     /*   changing all dots... */
  179.                             *xp = '_';      /*   ...to underscores */
  180.                     strcpy(xp, ".dir");    /* add extension for stat check */
  181.                     stat_val = stat(cdp, &statbuf);
  182.                     *xp = '\0';         /* remove extension for all else */
  183.                     if (stat_val) {     /* doesn't exist, so create */
  184. #else
  185.                     strcat(cdp, name);
  186.                     if (stat(cdp, &statbuf)) {  /* doesn't exist, so create */
  187. #endif
  188. #ifdef NO_MKDIR
  189.                         sprintf(command, "IFS=\" \t\n\" /bin/mkdir %s 2>/dev/null", cdp);
  190.                         if (system(command)) {
  191. #else
  192.                         if (MKDIR(cdp, PERMS) == -1) {
  193. #endif
  194.                             perror(cdp);
  195.                             free(cdp);
  196.                             fprintf(stderr, "Unable to process [%s]\n", filename);
  197.                             return (2);
  198.                         }
  199.                     } else if (!(statbuf.st_mode & S_IFDIR)) {
  200.                         fprintf(stderr, "%s:  exists but is not a directory\n",
  201.                                 cdp);
  202.                         free(cdp);
  203.                         fprintf(stderr, "unable to process [%s]\n", filename);
  204.                         return (2);
  205.                     }
  206. #ifdef VMS
  207.                     *xp = '/';  /* for now... (mkdir()) */
  208. #else /* !VMS */
  209. #ifdef MACOS
  210.                     strcat(cdp, ":");
  211. #else /* !MACOS */
  212.                     strcat(cdp, "/");
  213. #endif /* ?MACOS */
  214. #endif /* ?VMS */
  215.                 }               /***** FALL THROUGH to ':' case  **** */
  216.             case ':':           /* Discard DEC dev: or node:: name */
  217.                 pp = name;
  218.                 break;
  219.             case '.':                   /* DEC-20 generation number
  220.                                          * or MS-DOS type */
  221. #ifdef NUKE_DOTS
  222.                 if (++dc == 1)          /* Keep first dot */
  223.                     *pp++ = workch;
  224. #else
  225.                 ++dc;                   /* Not used, but what the hell. */
  226.                 *pp++ = workch;
  227. #endif
  228.                 break;
  229.             case ';':                   /* VMS generation or DEC-20 attrib */
  230.                 if (V_flag)             /* If requested, save VMS ";##" */
  231.                     *pp++ = workch;     /*  version info; else discard */
  232.                 else                    /*  everything starting with */
  233.                     done = TRUE;        /*  semicolon.  (Worry about */
  234.                 break;                  /*  DEC-20 later.) */
  235.             case '\026':                /* Control-V quote for special chars */
  236.                 quote = TRUE;           /* Set flag for next time. */
  237.                 break;
  238.             default:                    /* some other char */
  239.                 if (isdigit(workch))    /* '0'..'9' */
  240.                     *pp++ = workch;     /* accept them, no tests */
  241.                 else {
  242.                     if (workch == ' ')  /* change blanks to underscore */
  243.                         *pp++ = '_';
  244.                     else if (isprint(workch))   /* Other printable, just keep */
  245.                         *pp++ = workch;
  246.                 }
  247.             }                   /* switch */
  248.     }                           /* for loop */
  249.     *pp = '\0';                 /* Done with name, terminate it */
  250.  
  251. /*---------------------------------------------------------------------------
  252.     We COULD check for existing names right now, create a "unique" name, etc.
  253.     However, since other unzips don't do that...we won't bother.  Maybe an-
  254.     other day, ne?  If this went bad, the name'll either be nulled out (in
  255.     which case we'll return non-0) or following procedures won't be able to
  256.     create the extracted file, and other error msgs will result.
  257.   ---------------------------------------------------------------------------*/
  258.  
  259.     if (*name == '\0') {
  260.         fprintf(stderr, "conversion of [%s] failed\n", filename);
  261.         return (2);
  262.     }
  263.     if (dflag) {
  264. #ifdef VMS
  265.         *xp++ = ']';            /* proper end-of-dir-name delimiter */
  266.         if (xp == cdp) {        /* no path-name stuff, so... */
  267.             strcpy(filename, name);  /* copy file name into global */
  268.             cdp -= 2;           /*   prepare to free malloc'd space */
  269.         } else {                /* we've added path-name stuff... */
  270.             *xp = '\0';         /*   so terminate... */
  271.             dp = cdp;           /*   and convert to VMS subdir separators:  */
  272.             while (*++dp)       /*   (skip first char:  better not be "/") */
  273.                 if (*dp == '/') /*   change all slashes */
  274.                     *dp = '.';  /*     to dots */
  275.             cdp -= 2;           /*   include leading bracket and dot */
  276.             strcpy(filename, cdp);   /* copy VMS-style path name into global */
  277.             strcat(filename, name);  /* concatenate file name to global */
  278.         }
  279. #else
  280.         strcpy(filename, cdp);  /* Either "" or slash-terminated path */
  281.         strcat(filename, name); /* append file name to path name */
  282. #endif
  283.         free(cdp);
  284.     } else
  285.         strcpy(filename, name); /* copy converted name into global */
  286.  
  287. #if FILENAME_MAX < (FILNAMSIZ - 1)
  288.     /*
  289.      * Check the length of the file name and truncate if necessary.
  290.      */
  291.     if (FILENAME_MAX < strlen(filename)) {
  292.         fprintf(stderr, "warning:  filename too long--truncating.\n");
  293.         filename[FILENAME_MAX] = '\0';
  294.         fprintf(stderr, "[ %s ]\n", filename);
  295.         return (1);             /* 1:  warning error */
  296.     }
  297. #endif
  298.  
  299.     return (0);
  300. }
  301.